package com.thinkgem.jeesite.modules.fantasy.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.fantasy.entity.FantasyUser;
import com.thinkgem.jeesite.modules.fantasy.entity.NPCPosition;
import com.thinkgem.jeesite.modules.fantasy.entity.Talk;
import com.thinkgem.jeesite.modules.fantasy.service.impl.NPCPositionService;
import com.thinkgem.jeesite.modules.fantasy.service.impl.TalkService;
import com.thinkgem.jeesite.modules.sys.security.Principal;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;


@Controller
@RequestMapping(value = "fantasy/npc")
public class NpcController extends BaseController {
	@Autowired
	private NPCPositionService npcPositionService;
	@Autowired
	private TalkService talkService;

	@RequestMapping(value = "/queryNpc")
	public ModelAndView queryNpc(HttpServletRequest request) {
    	logger.debug("queryNpc()");
    	ModelAndView mv = new ModelAndView();
    	Principal principal = UserUtils.getPrincipal();
    	if(principal != null) {
    		Subject currentUser = SecurityUtils.getSubject();
			Session session = currentUser.getSession();
			FantasyUser user = (FantasyUser) session.getAttribute("operator");
			if (null == user) {
				logger.error("user == null");
				mv.setViewName("error/500");
				return mv;
			}
			String npcname = (String) request.getAttribute("npcname");
			logger.debug(npcname);
			List<NPCPosition> npcs = npcPositionService.queryNPCByName(npcname);
    		mv.addObject("npcs", npcs);
    		mv.setViewName("modules/fantasy/npcresult");
    	}
    	return mv;
	}
	/*
	 * 提升npc专属话语的显示几率
	 */
	@RequestMapping(value = "/talktoNpc")
	public ModelAndView talktoNpc(HttpServletRequest request) {
		String npcId = (String) request.getAttribute("npcId");
		logger.debug("npcId = " + npcId);
    	ModelAndView mv = new ModelAndView();
		List<Talk> npcTalks = talkService.getTalksByNpcId(npcId);
		List<Talk> normalTalks = talkService.getNormalTalks();
		Random r = new Random();
		//System.out.println(r.nextInt(npcTalks.size()));
		//System.out.println(npcTalks.get(r.nextInt(npcTalks.size())));
		List<Talk> tempTalks = new ArrayList<Talk>();
		if (npcTalks.size() > 0) {
			tempTalks.add(npcTalks.get(r.nextInt(npcTalks.size())));
		}
		if (normalTalks.size() > 0) {
			tempTalks.add(normalTalks.get(r.nextInt(normalTalks.size())));
		}
		String npcTalk = "";
		if (tempTalks.size() > 0) {
			npcTalk = tempTalks.get(r.nextInt(tempTalks.size())).getContent();
		}
		logger.debug(npcTalk);
		mv.addObject("npcTalk", npcTalk);
		mv.setViewName("modules/fantasy/talktonpc");
		return mv;
	}
	/*
	 * 提升npc专属话语的显示几率
	 */
	@RequestMapping("/talktoNpc2")
	@ResponseBody
	public Map<String, Object> talktoNpc2(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String npcId = (String) request.getAttribute("npcId");
		logger.debug("npcId = " + npcId);
		List<Talk> npcTalks = talkService.getTalksByNpcId(npcId);
		List<Talk> normalTalks = talkService.getNormalTalks();
		Random r = new Random();
		//System.out.println(r.nextInt(npcTalks.size()));
		//System.out.println(npcTalks.get(r.nextInt(npcTalks.size())));
		List<Talk> tempTalks = new ArrayList<Talk>();
		if (npcTalks.size() > 0) {
			tempTalks.add(npcTalks.get(r.nextInt(npcTalks.size())));
		}
		if (normalTalks.size() > 0) {
			tempTalks.add(normalTalks.get(r.nextInt(normalTalks.size())));
		}
		String npcTalk = "";
		if (tempTalks.size() > 0) {
			npcTalk = tempTalks.get(r.nextInt(tempTalks.size())).getContent();
		}
		logger.debug(npcTalk);
		map.put("message", npcTalk);
		return map;
	}
}
